<?php


namespace app\controller\machine;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 订单
 */
class Order extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 订单支付成功添加
     */
	public function api_machine_order_paySuccess($msg)
    {   
        $info = [
           'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
           'out_trade_no' => isset($msg['data']['out_trade_no'])?$msg['data']['out_trade_no']:'',
           'pay_type' => isset($msg['data']['pay_type'])?$msg['data']['pay_type']:'',
           'order_status' => isset($msg['data']['order_status'])?$msg['data']['order_status']:'',
        ];
        $goods = isset($msg['data']['goods'])?$msg['data']['goods']:'';
        $pathway = isset($msg['data']['pathway'])?$msg['data']['pathway']:'';
        $info['order_no'] = $this->common->createOutTradeNo('G');
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        if(!is_array($goods) || empty($goods)){
            return $this->outMessage(-1,'商品参数不正确');
        }
        if(!is_array($pathway) || empty($pathway)){
            return $this->outMessage(-1,'轨道参数不正确');
        }
        @$machine = $_SESSION['_EQUIPMENT_CODE_'.$info['code']];
        if(empty($machine) || !isset($machine)){
            return $this->outMessage(-1,'请先进行验证');
        }
        //获取商品信息
        $goods_list = [];
        foreach ($goods as $k => $v) {
            $equipment_goods = $this->table->find('equipment_goods',['equipment_code'=>$info['code'],'goods_id'=>$k]);
            if(empty($equipment_goods)){
                return $this->outMessage(-1,'该商品ID不存在');
            }
            $goods_list[$k]['order_no'] = $info['order_no'];
            $goods_list[$k]['goods_id'] = $k;
            $goods_list[$k]['goods_name'] = $equipment_goods['goods_name'];
            $goods_list[$k]['goods_pic'] = $equipment_goods['goods_pic'];
            $goods_list[$k]['goods_code'] = $equipment_goods['goods_code'];
            $goods_list[$k]['attribute'] = $equipment_goods['attribute'];
            $goods_list[$k]['number'] = $v;
            $goods_list[$k]['price'] = $equipment_goods['price'];
            $goods_list[$k]['sum_price'] = $equipment_goods['price']*$v;
            $goods_list[$k]['create_time'] = date('Y-m-d H:i:s',time());
        }
        //订单总价
        $sum_money = 0;
        $number = 0;
        foreach ($goods_list as $v) {
            $sum_money += $v['sum_price'];
            $number += $v['number'];
        }
        $order = [
            'order_no' => $info['order_no'],
            'out_trade_no' => $info['out_trade_no'],
            'equipment_id' => $machine['id'],
            'equipment_name' => $machine['name'],
            'merchant_id' => $machine['merchant_id'],
            'merchant_name' => $machine['merchant_name'],
            'itoc_id' => $machine['itoc_id'],
            'itoc_name' => $machine['itoc_name'],
            'region_id'=> $machine['region_id'],
            'region_name'=> $machine['region_name'],
            'pay_type' => $info['pay_type'],
            'order_money' => $sum_money,
            'order_status' => $info['order_status'],
            'create_time' => date('Y-m-d H:i:s',time()),
            'pay_time' => date('Y-m-d H:i:s',time()),
        ];
        //插入订单表
        $res = $this->table->insert('order',$order);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        //循环插入订单商品表
        foreach ($goods_list as $k => $v) {
            $res = $this->table->insert('order_goods',$goods_list[$k]);
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
        }
        //修改设备售出数量
        $machine = $this->table->find('equipment_list',['code'=>$info['code']]);
        $update_data = [
            'sales'=> $machine['sales']+$number,
            'sum_money'=> $machine['sum_money']+$sum_money
        ];
        $this->table->update('equipment_list',['code'=>$info['code']],$update_data);
        //修改设备轨道商品数量
        foreach ($pathway as $k => $v) {
            $pathway_info = $this->table->find('equipment_goods',['equipment_code'=>$info['code'],'pathway_id'=>$k]);
            if(empty($equipment_goods)){
                return $this->outMessage(-1,'该轨道不存在');
            }
            $this->table->update('equipment_goods',['id' => $pathway_info['id']],['surplus_number'=>$pathway_info['surplus_number']-$v]);
        }
		return $this->outMessage(10000,'提交成功');
	}





}
